#include "screenshottool.h"
#include "buttongroup.h"


#include <QHBoxLayout>
#include <QToolButton>
#include <QPainter>
#include <QCursor>
#include <QDebug>
#include <QDialog>

ScreenshotTool::ScreenshotTool(QWidget *parent) :
    QWidget(parent),
    m_Ok(QIcon("://image/copy"), tr("确定"), tr("复制到剪切板"), this),
    m_Cancel(QIcon("://image/close"), tr("取消"), tr("取消截屏"), this),
    m_Pin(QIcon("://image/ding"), tr("钉一下"), tr("钉在屏幕上"), this),
    m_SavePixmap(QIcon("://image/save"), tr("保存"), tr("保存截图文件"), this),
    m_Undo(QIcon("://image/undo"), tr("撤销"), tr("撤销"), this),
    m_Redo(QIcon("://image/redo"), tr("重做"), tr("重做"), this),
    m_nCursor(Qt::ArrowCursor),
    m_nShape(None)
{
    Init();
}

ScreenshotTool::~ScreenshotTool()
{
}

void ScreenshotTool::ok()
{
    emit screenshotComplete(true);
}

void ScreenshotTool::cancel()
{
    emit screenshotComplete(false);
}

void ScreenshotTool::pin()
{
    emit pasteOnTheScreen();
}

void ScreenshotTool::drawLine(bool checked)
{
    if (Line != m_nShape && None != m_nShape && !checked)
    {
        return;
    }
    m_nShape = checked ? Line : None;
    emit drawShape(m_nShape);
//    m_drawLineProperty.show();
//    m_drawRectProperty.hide();
//    m_drawTextProperty.hide();
}

void ScreenshotTool::drawRect(bool checked)
{
    if (Rect != m_nShape && None != m_nShape && !checked)
    {
        return;
    }
    m_nShape = checked ? Rect : None;
    emit drawShape(m_nShape);
//    m_drawLineProperty.hide();
//    m_drawRectProperty.show();
//    m_drawTextProperty.hide();
}

void ScreenshotTool::drawText(bool checked)
{
    if (Text != m_nShape && None != m_nShape && !checked)
    {
        return;
    }
    m_nShape = checked ? Text : None;
    emit drawShape(m_nShape);
//    m_drawLineProperty.hide();
//    m_drawRectProperty.hide();
    //    m_drawTextProperty.show();
}

void ScreenshotTool::savePixmap()
{
    emit saveScreenshot();
}

void ScreenshotTool::EnableUndo(bool bEnable)
{
    m_Undo.setEnabled(bEnable);
}

void ScreenshotTool::EnableRedo(bool bEnable)
{
    m_Redo.setEnabled(bEnable);
}

void ScreenshotTool::mousePressEvent(QMouseEvent *event)
{
    Q_UNUSED(event)
//    QWidget::mousePressEvent(event);
}

void ScreenshotTool::mouseReleaseEvent(QMouseEvent *event)
{
    QWidget::mouseReleaseEvent(event);
}

void ScreenshotTool::mouseDoubleClickEvent(QMouseEvent *event)
{
    QWidget::mouseDoubleClickEvent(event);
}

void ScreenshotTool::mouseMoveEvent(QMouseEvent *event)
{
    QWidget::mouseMoveEvent(event);
}

void ScreenshotTool::enterEvent(QEvent *event)
{
    QWidget::enterEvent(event);

    m_nCursor = cursor().shape();
    setCursor(Qt::ArrowCursor);

}

void ScreenshotTool::leaveEvent(QEvent *event)
{
    QWidget::leaveEvent(event);

    setCursor(m_nCursor);
}

void ScreenshotTool::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
//    painter.save();
//    painter.fillRect(rect(), QBrush(QColor(243, 243, 243)));
//    painter.restore();
    //painter.save();
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);
    painter.setBrush(QBrush(QColor(243, 243, 243)));
    painter.drawRoundedRect(this->rect(), 10, 10);
    //painter.restore();
    QWidget::paintEvent(event);
}

void ScreenshotTool::showEvent(QShowEvent *event)
{
    emit ShowWindow();
    QWidget::showEvent(event);
}

void ScreenshotTool::hideEvent(QHideEvent *event)
{
    emit HideWindow();
    QWidget::hideEvent(event);
}

void ScreenshotTool::Init()
{
    //setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint /*| Qt::Popup*/);
    setAttribute(Qt::WA_QuitOnClose, false);
    QHBoxLayout *pLayout = new QHBoxLayout(this);
    pLayout->setContentsMargins(3, 3, 3, 3);
    setLayout(pLayout);
//    QToolButton *LineButton = new QToolButton(this);
//    LineButton->setText("——");
//    pLayout->addWidget(LineButton);
//    connect(LineButton, &QToolButton::clicked, this, &ScreenshotTool::drawLine);

    pLayout->addWidget(&m_Pin);
    connect(&m_Pin, &QToolButton::clicked, this, &ScreenshotTool::pin);

    pLayout->addWidget(&m_SavePixmap);
    connect(&m_SavePixmap, &QToolButton::clicked, this, &ScreenshotTool::savePixmap);

    ButtonGroup* pButtonGroup = new ButtonGroup(this);
    //画线按钮
    CCustomToolButton* pDrawLine = new CCustomToolButton(QIcon("://image/line"), tr("线"), tr("自由线"), this);
    pDrawLine->setCheckable(true);
    pLayout->addWidget(pDrawLine);
    connect(pDrawLine, &QToolButton::toggled, this, &ScreenshotTool::drawLine);
    pButtonGroup->addButton(pDrawLine);

    CCustomToolButton* pDrawRect = new CCustomToolButton(QIcon("://image/rect"), tr("矩形"), tr("矩形"), this);
    pDrawRect->setCheckable(true);
    pLayout->addWidget(pDrawRect);
    connect(pDrawRect, &QToolButton::toggled, this, &ScreenshotTool::drawRect);
    pButtonGroup->addButton(pDrawRect);

    CCustomToolButton* pDrawText = new CCustomToolButton(QIcon("://image/text"), tr("文本"), tr("文本"), this);
    pDrawText->setCheckable(true);
    pLayout->addWidget(pDrawText);
    connect(pDrawText, &QToolButton::toggled, this, &ScreenshotTool::drawText);
    pButtonGroup->addButton(pDrawText);

    pLayout->addWidget(&m_Undo);
    connect(&m_Undo, &QToolButton::clicked, this, &ScreenshotTool::Undo);

    pLayout->addWidget(&m_Redo);
    connect(&m_Redo, &QToolButton::clicked, this, &ScreenshotTool::Redo);

    pLayout->addWidget(&m_Ok);
    connect(&m_Ok, &QToolButton::clicked, this, &ScreenshotTool::ok);

    pLayout->addWidget(&m_Cancel);
    connect(&m_Cancel, &QToolButton::clicked, this, &ScreenshotTool::cancel);

    //setStyleSheet("QWidget{border-style: flat;}");

    //初始隐藏
//    m_drawLineProperty.hide();
//    m_drawRectProperty.hide();
//    m_drawTextProperty.hide();
}
